/**
 * Copyright 2009 the original author or authors.
 *
 * Licensed under the GPL License, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.guardianapis.api.transformers;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.guardianapis.api.client.GuardianConstants;
import com.guardianapis.api.client.SearchRequest;
import com.guardianapis.api.util.DateFormatter;
import com.guardianapis.api.util.URLEncoder;
import com.guardianapis.api.util.URLEncoderOptions;

public class SearchRequestTransformer implements RequestTransformer {

	/*******************************************************************************
	 * Constants
	 *******************************************************************************/

	private Log log = LogFactory.getLog(SearchRequestTransformer.class);
	
	private static final String SEARCH_STRING_PARAM = "q";
	private static final String BEFORE_PARAM = "before";
	private static final String AFTER_PARAM = "after";
	private static final String FORMAT_PARAM = "format";
	private static final String CONTENT_TYPE_PARAM = "content-type";
	private static final String COUNT_PARAM = "count";
	private static final String START_PAGE_PARAM = "start-index";
	private static final String FILTER_PARAM = "filter";

	/*******************************************************************************
	 * Properties
	 *******************************************************************************/

	private URLEncoder urlEncoder;
	private URLEncoder filterUrlEncoder;
	
	/*******************************************************************************
	 * Constructors
	 *******************************************************************************/

	public SearchRequestTransformer() {
		URLEncoderOptions encoderOptions = new URLEncoderOptions();
		encoderOptions.setEncodeForwardSlashes(false);
		this.urlEncoder = new URLEncoder();
		this.filterUrlEncoder = new URLEncoder(encoderOptions);
	}
	
	/*******************************************************************************
	 * Implementing RequestTransformer
	 *******************************************************************************/
	
	public String transformToQueryString(Object pRequest) {
		
		StringBuilder queryStringResult = new StringBuilder();
		if (pRequest instanceof SearchRequest) {
			SearchRequest searchRequest = (SearchRequest) pRequest;
			
			if (searchRequest.getSearchString() != null && !searchRequest.getSearchString().equals("")) {
				queryStringResult.append(GuardianConstants.URL_PARAM_SEPERATOR + SEARCH_STRING_PARAM + "=" + urlEncoder.encode(searchRequest.getSearchString()));
			}
			if (searchRequest.getFormat() != null) {
				queryStringResult.append(GuardianConstants.URL_PARAM_SEPERATOR + FORMAT_PARAM + "=" + searchRequest.getFormat().toString().toLowerCase());
			}
			if (searchRequest.getAfter() != null) {
				queryStringResult.append(GuardianConstants.URL_PARAM_SEPERATOR + AFTER_PARAM + "=" + DateFormatter.formatDate(searchRequest.getAfter()));
			}
			if (searchRequest.getBefore() != null) {
				queryStringResult.append(GuardianConstants.URL_PARAM_SEPERATOR + BEFORE_PARAM + "=" + DateFormatter.formatDate(searchRequest.getBefore()));
			}
			if (searchRequest.getContentType() != null && !searchRequest.getContentType().equals("")) {
				queryStringResult.append(GuardianConstants.URL_PARAM_SEPERATOR + CONTENT_TYPE_PARAM + "=" + searchRequest.getContentType().toString().toLowerCase());
			}
			if (searchRequest.getCount() != null) {
				queryStringResult.append(GuardianConstants.URL_PARAM_SEPERATOR + COUNT_PARAM + "=" + searchRequest.getCount());
			}
			if (searchRequest.getStartPage() != null) {
				queryStringResult.append(GuardianConstants.URL_PARAM_SEPERATOR + START_PAGE_PARAM + "=" + searchRequest.getStartPage());
			}
			if (searchRequest.getFilters() != null && searchRequest.getFilters().size() > 0) {
				for (String currentFilter : searchRequest.getFilters()) {
					queryStringResult.append(GuardianConstants.URL_PARAM_SEPERATOR + FILTER_PARAM + "=" + filterUrlEncoder.encode(currentFilter));	
				}
			}
		}
		else {
			log.error("Source object is of type: " + pRequest.getClass() + " - expecting SearchRequest");
		}
		
		if (log.isDebugEnabled()) {
			log.debug("Source request: " + pRequest + " was transformed to query string: " + queryStringResult.toString());
		}
		
		return queryStringResult.toString();
	}
	
}
